package view;

import model.User;
import service.UserResigter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Resgister extends JFrame implements ActionListener {
    JLabel jl, jl2, jl3;
    JButton jb;
    JTextField jtf;
    JPasswordField jpf;

    public static void main(String[] args) {
        new Resgister();
    }

    public Resgister() {
        jl = new JLabel(new ImageIcon(getClass().getResource("/1.png")));
        jl.setBounds(0, 0, 713, 422);
        add(jl);

        jl2 = new JLabel("账号");
        jl2.setBounds(170, 430, 100, 80);
        jl2.setFont(new Font("宋体", Font.BOLD, 20));
        add(jl2);

        jl3 = new JLabel("密码");
        jl3.setBounds(170, 480, 100, 80);
        jl3.setFont(new Font("宋体", Font.BOLD, 20));
        add(jl3);

        jpf = new JPasswordField();
        jpf.setBounds(300, 505, 200, 30);
        add(jpf);


        jtf = new JTextField();
        jtf.setBounds(300, 455, 200, 30);
        add(jtf);

        jb = new JButton("注册");
        jb.setFont(new Font("宋体", Font.BOLD, 20));
        jb.setBounds(320, 560, 100, 40);
        jb.addActionListener(this);
        add(jb);

        setTitle("用户注册");
        setLayout(null);
        setLocation(500, 80);
        setSize(700, 650);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int flag = 0;
        if (e.getSource() == jb) {
            User user = new User(Integer.parseInt(jtf.getText()), jpf.getText());
            flag = new UserResigter().resigter(user);
            if (flag == 1) {
                JOptionPane.showMessageDialog(null, "注册成功，请关闭进入登录界面", "Successful", JOptionPane.PLAIN_MESSAGE);
            } else if (flag == 2) {
                JOptionPane.showMessageDialog(null, "该用户已存在,请重新输入", "error", JOptionPane.ERROR_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "系统错误", "warning", JOptionPane.ERROR_MESSAGE);
            }
        }

    }
}
